/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.check.checkers;

import cn.ac.iie.di.ban.news.server.check.CheckResult;
import cn.ac.iie.di.ban.news.server.check.Checker;
import java.text.SimpleDateFormat;

/**
 *
 * @author zwlin
 */
public class TimeDelayChecker implements Checker<String, Integer> {

    private static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     *
     * @param src time yyyyMMddHHmmss
     * @param target delay time second
     * @return
     */
    @Override
    public CheckResult check(String src, Integer target) {
        try {
            if (null == src || (src = src.trim()).isEmpty()) {
                return new CheckResult();
            }
            long t = formatter.parse(src).getTime();
            if (target > 0 && System.currentTimeMillis() - t > target * 1000) {
                return new CheckResult("record delay more than " + target + "s.");
            }
            return new CheckResult();
        } catch (Exception ex) {
            return new CheckResult("time format error.");
        }
    }

}
